package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.TaskDocumentDao;
import cn.topevery.garden.entity.po.TaskDocument;
import cn.topevery.garden.entity.query.TaskDocumentQuery;
import cn.topevery.garden.entity.vo.TaskDocumentVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import hw.topevery.framework.entity.KeyValue;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 内业检查
 */
@Repository
public class TaskDocumentDaoImpl extends BaseDaoImpl<TaskDocument> implements TaskDocumentDao {
    @Override
    public <P extends DbCsp> List<TaskDocument> getSearch(P para) {
        return null;
    }


    @Override
    public DataPager<TaskDocumentVO> listPage(TaskDocumentQuery query) {
        String table = "t_task_document ttd";

        String columns = "ttd.*";

        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("ttd.c_id desc");
        sqlQueryMate
                .where(true, "ttd.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(StringUtils.isNotBlank(query.getSectionId()), "ttd.c_section_id", ScriptConditionEnum.Eq, query.getSectionId())
                .where(query.getCompanyId() != null, "ttd.c_company_id", ScriptConditionEnum.Eq, query.getCompanyId())
                .where(StringUtils.isNotBlank(query.beginDate), "ttd.c_evaluation_time", ScriptConditionEnum.GreaterThanEq, query.beginDate)
                .where(StringUtils.isNotBlank(query.endDate), "ttd.c_evaluation_time", ScriptConditionEnum.LessThanEq, query.endDate)
                .where(query.getFinished() != null && query.getFinished(), "ttd.c_finished", ScriptConditionEnum.Eq, query.getFinished())
                .where(query.getCheckBeginDate() != null, "ttd.c_check_time", ScriptConditionEnum.GreaterThanEq, query.getCheckBeginDate())
                .where(query.getCheckEndDate() != null, "ttd.c_check_time", ScriptConditionEnum.LessThanEq, query.getCheckEndDate())
        ;

        return getDataPager(sqlQueryMate, pageSize, pageIndex, TaskDocumentVO.class);
    }

    @Override
    public TaskDocument getEntityByColumn(String key, String value) {
        KeyValue<String> map = new KeyValue<>();
        map.key = key;
        map.value = value;
        return getEntityByColumnEqValue(map);
    }

    @Override
    public DataPager<TaskDocumentVO> pageMyTask(TaskDocumentQuery query) {
        String columns = "ttd.*";
        String table = "t_task_document ttd\n" +
                "         LEFT JOIN t_task_user ttu ON ttd.c_id = ttu.c_task_id AND ttu.c_db_status = 0";
        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("ttd.c_id desc");
        sqlQueryMate
                .where(true, "ttd.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(query.getTaskOwnerId() != null, "ttu.c_evaluation_user_id", ScriptConditionEnum.Eq, query.getTaskOwnerId())
                .where(query.getFinished() != null && query.getFinished(), "ttd.c_finished", ScriptConditionEnum.Eq, query.getFinished())
        ;
        return getDataPager(sqlQueryMate, pageSize, pageIndex, TaskDocumentVO.class);
    }
}
